#==============================================================================
# ** Game_Actor
#------------------------------------------------------------------------------
#  This class handles the actor. It's used within the Game_Actors class
#  ($game_actors) and refers to the Game_Party class ($game_party).
#==============================================================================

class Game_Actor < Game_Battler
    #--------------------------------------------------------------------------
    # * Public Instance Variables
    #--------------------------------------------------------------------------
    attr_reader   :character_name           # character file name
    attr_reader   :character_hue            # character hue
    attr_reader   :class_id                 # class ID
    attr_reader   :weapon_id                # weapon ID
    attr_reader   :armor1_id                # shield ID
    attr_reader   :armor2_id                # helmet ID
    attr_reader   :armor3_id                # body armor ID
    attr_reader   :armor4_id                # accessory ID
    attr_reader   :level                    # level
    attr_reader   :exp                      # EXP
    attr_reader   :equip_skills                   # skills
    #--------------------------------------------------------------------------
    # * Object Initialization
    #     actor_id : actor ID
    #--------------------------------------------------------------------------
    def initialize(actor_id)
      super()
      setup(actor_id)
    end
    #--------------------------------------------------------------------------
    # * Setup
    #     actor_id : actor ID
    #--------------------------------------------------------------------------
    def setup(actor_id)
      actor = $data_actors[actor_id]
      @actor_id = actor_id
      @name = actor.name
      @character_name = actor.character_name
      @character_hue = actor.character_hue
      @battler_name = actor.battler_name
      @battler_hue = actor.battler_hue
      @class_id = actor.class_id
      @weapon_id = actor.weapon_id
      @armor1_id = actor.armor1_id
      @armor2_id = actor.armor2_id
      @armor3_id = actor.armor3_id
      @armor4_id = actor.armor4_id
      @level = actor.initial_level
      @exp_list = Array.new(101)
      make_exp_list
      @exp = @exp_list[@level]
      @equip_skills = []
      @hp = maxhp
      @sp = 0
      @states = []
      $game_temp.equip_props_store.make_plus(@weapon_id, 0)
      $game_temp.equip_props_store.make_plus(@armor1_id, 1)
      $game_temp.equip_props_store.make_plus(@armor2_id, 2)
      $game_temp.equip_props_store.make_plus(@armor3_id, 3)
      $game_temp.equip_props_store.make_plus(@armor4_id, 4)
      # Set Innate_Skill
      case actor_id
        when 1
          innate_skills.push(Innate_Skill_001.new(self))
          innate_skills.push(Innate_Skill_002.new(self))
          innate_skills.push(Innate_Skill_003.new(self))
          innate_skills.push(Innate_Skill_004.new(self))
        when 2
          innate_skills.push(Innate_Skill_005.new(self))
          innate_skills.push(Innate_Skill_006.new(self))
          innate_skills.push(Innate_Skill_007.new(self))
          innate_skills.push(Innate_Skill_008.new(self))
        when 3
          innate_skills.push(Innate_Skill_HolyBeat.new(self))
          innate_skills.push(Innate_Skill_HealingPrayer.new(self))
          innate_skills.push(Innate_Skill_PureHeart.new(self))
          innate_skills.push(Innate_Skill_GalacticSpiritGift.new(self))
      end
      GlobalMethods.addLog("init actor=" + @name)
    end
    #--------------------------------------------------------------------------
    # * Get Actor ID
    #--------------------------------------------------------------------------
    def id
      return @actor_id
    end
    #--------------------------------------------------------------------------
    # * Get Basic Maximum HP
    #--------------------------------------------------------------------------
    def base_maxhp
      return $data_actors[@actor_id].parameters[0, @level]
    end
    #--------------------------------------------------------------------------
    # * Get Basic Maximum SP
    #--------------------------------------------------------------------------
    def base_maxsp
      return $data_actors[@actor_id].parameters[1, @level]
    end
    #--------------------------------------------------------------------------
    # * Get Basic Strength
    #--------------------------------------------------------------------------
    def base_str
      return $data_actors[@actor_id].parameters[2, @level]
    end#--------------------------------------------------------------------------
    # * Get Basic Dexterity
    #--------------------------------------------------------------------------
    def base_dex
      return $data_actors[@actor_id].parameters[3, @level]
    end
    #--------------------------------------------------------------------------
    # * Get Basic Agility
    #--------------------------------------------------------------------------
    def base_agi
      return $data_actors[@actor_id].parameters[4, @level]
    end
    #--------------------------------------------------------------------------
    # * Get Basic Intelligence
    #--------------------------------------------------------------------------
    def base_int
      return $data_actors[@actor_id].parameters[5, @level]
    end
    #--------------------------------------------------------------------------
    # * Get Basic Attack Power
    #--------------------------------------------------------------------------
    def base_atk
      weapon = $data_weapons[@weapon_id]
      return weapon != nil ? weapon.atk : 0
    end
    #--------------------------------------------------------------------------
    # * Get Basic Physical Defense
    #--------------------------------------------------------------------------
    def base_pdef
      armor1 = $data_armors[@armor1_id]
      armor2 = $data_armors[@armor2_id]
      armor3 = $data_armors[@armor3_id]
      armor4 = $data_armors[@armor4_id]
      pdef1 = armor1 != nil ? armor1.pdef : 0
      pdef2 = armor2 != nil ? armor2.pdef : 0
      pdef3 = armor3 != nil ? armor3.pdef : 0
      pdef4 = armor4 != nil ? armor4.pdef : 0
      return pdef1 + pdef2 + pdef3 + pdef4
    end
    def base_mdef
      return 0
    end
    #--------------------------------------------------------------------------
    # * Get Index
    #--------------------------------------------------------------------------
    def index
      return $game_party.actors.index(self)
    end
    #--------------------------------------------------------------------------
    # * Calculate EXP
    #--------------------------------------------------------------------------
    def make_exp_list
      actor = $data_actors[@actor_id]
      @exp_list[1] = 0
      pow_i = 2.4 + actor.exp_inflation / 100.0
      for i in 2..100
        if i > actor.final_level
          @exp_list[i] = 0
        else
          n = actor.exp_basis * ((i + 3) ** pow_i) / (5 ** pow_i)
          @exp_list[i] = @exp_list[i-1] + Integer(n)
        end
      end
    end
    #--------------------------------------------------------------------------
    # * Get Element Revision Value
    #     element_id : element ID
    #--------------------------------------------------------------------------
    def element_rate(element_id)
      return 0
    end
    #--------------------------------------------------------------------------
    # * Get State Effectiveness
    #--------------------------------------------------------------------------
    def state_ranks
      return $data_classes[@class_id].state_ranks
    end
    #--------------------------------------------------------------------------
    # * Determine State Guard
    #     state_id : state ID
    #--------------------------------------------------------------------------
    def state_guard?(state_id)
      return false
    end
    #--------------------------------------------------------------------------
    # * Get Normal Attack Element
    #--------------------------------------------------------------------------
    def element_set
      weapon = $data_weapons[@weapon_id]
      return weapon != nil ? weapon.element_set : []
    end
    #--------------------------------------------------------------------------
    # * Get Normal Attack State Change (+)
    #--------------------------------------------------------------------------
    def plus_state_set
      weapon = $data_weapons[@weapon_id]
      return weapon != nil ? weapon.plus_state_set : []
    end
    #--------------------------------------------------------------------------
    # * Get Normal Attack State Change (-)
    #--------------------------------------------------------------------------
    def minus_state_set
      weapon = $data_weapons[@weapon_id]
      return weapon != nil ? weapon.minus_state_set : []
    end
    #--------------------------------------------------------------------------
    # * Get Class Name
    #--------------------------------------------------------------------------
    def class_name
      return $data_classes[@class_id].name
    end
    #--------------------------------------------------------------------------
    # * Get EXP String
    #--------------------------------------------------------------------------
    def exp_s
      return @exp_list[@level+1] > 0 ? @exp.to_s : "-------"
    end
    #--------------------------------------------------------------------------
    # * Get Next Level EXP String
    #--------------------------------------------------------------------------
    def next_exp_s
      return @exp_list[@level+1] > 0 ? @exp_list[@level+1].to_s : "-------"
    end
    #--------------------------------------------------------------------------
    # * Get Until Next Level EXP String
    #--------------------------------------------------------------------------
    def next_rest_exp_s
      return @exp_list[@level+1] > 0 ?
        (@exp_list[@level+1] - @exp).to_s : "-------"
    end
    def equip(equip_type, id)
      case equip_type
      when 0  # Weapon
        if id == 0 or $game_party.weapon_number(id) > 0
          $game_party.gain_weapon(@weapon_id, 1)
          update_equip_skill(@weapon_id, 1, false)
          @weapon_id = id
          $game_party.lose_weapon(id, 1)
          update_equip_skill(id, 1, true)
          weapon = $data_weapons[@weapon_id]
        end
      when 1  # Shield
        if id == 0 or $game_party.armor_number(id) > 0
          $game_party.gain_armor(@armor1_id, 1)
          update_equip_skill(@armor1_id, 2, false)
          @armor1_id = id
          $game_party.lose_armor(id, 1)
          update_equip_skill(id, 2, true)
        end
      when 2  # Head
        if id == 0 or $game_party.armor_number(id) > 0
          $game_party.gain_armor(@armor2_id, 1)
          update_equip_skill(@armor2_id, 2, false)
          @armor2_id = id
          $game_party.lose_armor(id, 1)
          update_equip_skill(id, 2, true)
        end
      when 3  # Body
        if id == 0 or $game_party.armor_number(id) > 0
          $game_party.gain_armor(@armor3_id, 1)
          update_equip_skill(@armor3_id, 2, false)
          @armor3_id = id
          $game_party.lose_armor(id, 1)
          update_equip_skill(id, 2, true)
        end
      when 4  # Accessory
        if id == 0 or $game_party.armor_number(id) > 0
          $game_party.gain_armor(@armor4_id, 1)
          update_equip_skill(@armor4_id, 2, false)
          @armor4_id = id
          $game_party.lose_armor(id, 1)
          update_equip_skill(id, 2, true)
        end
      end
    end
    # 这里统计装备技能
    def update_equip_skill(equip_id, type, is_equiped)
      if equip_id == 0
        return
      end
      GlobalMethods.addLog('update_equip_skill, equip_id=' + equip_id.to_s + ', type=' + type.to_s + ', is_equiped=' + is_equiped.to_s)
      helper = Equip_Skill_Helper.new(equip_id, type)
      GlobalMethods.addLog('helper length=' + helper.skills.size.to_s)
      for i in 0 ... helper.skills.size
        sk = helper.skills[i]
        le = helper.levels[i]
        if is_equiped == false
          le = -le
        end
        index = -1
        for k in 0 ... @equip_skills.size
          if sk == @equip_skills[k].id
            index = k
            @equip_skills[k].change_level(le)
            break
          end
        end
        if index == -1 && is_equiped
          new_equip_skill = helper.build_skill(self, sk, le)
          @equip_skills.push(new_equip_skill)
        end
        if index > -1 && @equip_skills[index].level <= 0
          @equip_skills.delete_at(index)
        end
        str = ''
        GlobalMethods.addLog('————CUR SKILL————')
        for item in @equip_skills
          str += item.name + " - " + item.level.to_s + '\n'
        end
        GlobalMethods.addLog(str)
        GlobalMethods.addLog('————————————')
      end
    end
    #--------------------------------------------------------------------------
    # * Determine if Equippable
    #     item : item
    #--------------------------------------------------------------------------
    def equippable?(item)
      return true
    end
    #--------------------------------------------------------------------------
    # * Change EXP
    #     exp : new EXP
    #--------------------------------------------------------------------------
    def exp=(exp)
      @exp = [[exp, 9999999].min, 0].max
    end
    #--------------------------------------------------------------------------
    # * Change Level
    #     level : new level
    #--------------------------------------------------------------------------
    def level=(level)
      # Check up and down limits
      level = [[level, $data_actors[@actor_id].final_level].min, 1].max
      # Change EXP
      self.exp = @exp_list[level]
    end
    #--------------------------------------------------------------------------
    # * Change Name
    #     name : new name
    #--------------------------------------------------------------------------
    def name=(name)
      @name = name
    end
    #--------------------------------------------------------------------------
    # * Change Class ID
    #     class_id : new class ID
    #--------------------------------------------------------------------------
    def class_id=(class_id)
      if $data_classes[class_id] != nil
        @class_id = class_id
        # Remove items that are no longer equippable
        unless equippable?($data_weapons[@weapon_id])
          equip(0, 0)
        end
        unless equippable?($data_armors[@armor1_id])
          equip(1, 0)
        end
        unless equippable?($data_armors[@armor2_id])
          equip(2, 0)
        end
        unless equippable?($data_armors[@armor3_id])
          equip(3, 0)
        end
        unless equippable?($data_armors[@armor4_id])
          equip(4, 0)
        end
      end
    end
    #--------------------------------------------------------------------------
    # * Change Graphics
    #     character_name : new character file name
    #     character_hue  : new character hue
    #     battler_name   : new battler file name
    #     battler_hue    : new battler hue
    #--------------------------------------------------------------------------
    def set_graphic(character_name, character_hue, battler_name, battler_hue)
      @character_name = character_name
      @character_hue = character_hue
      @battler_name = battler_name
      @battler_hue = battler_hue
    end
    #--------------------------------------------------------------------------
    # * Get Battle Screen X-Coordinate
    #--------------------------------------------------------------------------
    def screen_x
      # Return after calculating x-coordinate by order of members in party
      if self.index != nil
        return self.index * 160 + 80
      else
        return 0
      end
    end
    #--------------------------------------------------------------------------
    # * Get Battle Screen Y-Coordinate
    #--------------------------------------------------------------------------
    def screen_y
      return 464
    end
    #--------------------------------------------------------------------------
    # * Get Battle Screen Z-Coordinate
    #--------------------------------------------------------------------------
    def screen_z
      # Return after calculating z-coordinate by order of members in party
      if self.index != nil
        return 4 - self.index
      else
        return 0
      end
    end
  end
